//
//  WPPresentByModal.m
//  转场动画
//
//  Created by WGP on 16/8/3.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "CustomPresentAnimationController.h"

@interface CustomPresentAnimationController ()

@property (nonatomic, assign) BOOL presenting; 

@end
@implementation CustomPresentAnimationController

- (instancetype)initWithPresenting:(BOOL)presenting;
{
    if (self = [super init]) {
        
        self.presenting = presenting;
    }
    return self;
}

- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    if (self.presenting) {
        [self presentingAnimation:transitionContext];
    }
    else {
        [self dismissingAnimation:transitionContext];
    }
}


// present视图控制器的自定义动画(modal出视图控制器)
- (void)presentingAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{

    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    CGRect finalFrameForVC = [transitionContext finalFrameForViewController:toViewController];
    UIView *containerView = [transitionContext containerView];
    CGRect bounds = [UIScreen mainScreen].bounds;
    toViewController.view.frame = CGRectOffset(finalFrameForVC, bounds.size.width, 0);
    [containerView addSubview:toViewController.view];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        //        fromViewController.view.alpha = 0.5;
        toViewController.view.frame = finalFrameForVC;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
        //        fromViewController.view.alpha  = 1.0;
    }];
}

// dissmiss视图控制器的自定义动画(关闭modal视图控制器)
- (void)dismissingAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    CGRect finalFrameForVC = [transitionContext finalFrameForViewController:toViewController];
    UIView *containerView = [transitionContext containerView];
    
    CGRect bounds = [UIScreen mainScreen].bounds;
    toViewController.view.frame = finalFrameForVC;
    
    
    [containerView addSubview:toViewController.view];
    [containerView sendSubviewToBack:toViewController.view];
    
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        fromViewController.view.frame = CGRectOffset(toViewController.view.frame, bounds.size.width, 0);
    
        
    } completion:^(BOOL finished) {
        
        // 注意要把视图移除
        [fromViewController.view removeFromSuperview];
        [transitionContext completeTransition:YES];
        
    }];
}

@end
















